/**
 * Created by Huang_Xiao_Lin on 2017/6/5.
 */
app.controller('homeCtrl',function ($rootScope, $state, $scope, $http, $localStorage) {});
app.controller('tableCtrl',function ($rootScope, $state, $scope, $http, $localStorage,$filter,$location,$timeout,Excel,dataService) {
    $scope.getOrder =function () {
        $http.get(API('huanbi/findBankOrder')).success(function (result) {
            if(result.successful){
                $scope.orderList = result.data;
                $scope.orderList.sort(function(a,b){
                    return b.orderDate-a.orderDate});
                //$scope.yiYuanNum = 0;$scope.wuJiaoNum = 0;$scope.yiJiaoNum = 0;
                // angular.forEach(result.data,function (data, index, array) {
                // $scope.yiYuanNum += (!data.yiYuanNum ? 0 : parseInt(data.yiYuanNum));
                // $scope.wuJiaoNum += (!data.wuJiaoNum ? 0 : parseInt(data.wuJiaoNum));
                // $scope.yiJiaoNum += (!data.yiJiaoNum ? 0 : parseInt(data.yiJiaoNum));
                // })
            }else {
                //$state.go('login');
            }
        });
    };
    $scope.getOrder();
    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        //下面是在table render完成后执行的js
        $scope.table = $("#dataTables-example").DataTable({
            "lengthMenu": [[10, 25, 50,100, -1], [10, 25, 50, 100,"全部"]],
            bSort:false,
            "order": [[ 4, "asc" ]],
            "language": {
                "info": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录。总计:一元<b id='yiYuanNum'>"+$scope.yiYuanNum+"枚</b>，五角<b id='wuJiaoNum'>"+$scope.wuJiaoNum+"枚</b>，一角<b id='yiJiaoNum'>"+$scope.yiJiaoNum+"枚</b>"
            }
        });
    });
    $scope.filterType = function (orderStatus) {
        switch (orderStatus) {
            case "未领":
                return 0;
            case  "已领":
                return 1;
            case "逾期":
                return 2;
        }
    }

    $scope.today = $filter('date')(new Date(), "yyyy-MM-dd");
    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            if($location.path()=='/home/table'){
                var select_type  = $("#recordType option:selected").val()//选择状态
                var type = $scope.filterType(data[5]);//状态行
                var select_date1 = $("#dtp_input1").val();//选择日期
                var select_date2 = $("#dtp_input2").val();//选择日期
                var date = (data[4]).substring(0,10);//日期行
                var select_bankName =  $('#dtp_input3').val();
                var bankname = data[3];
                if(select_date1 != '' && select_date2 != '' && typeof(select_date1 ) != "undefined" && typeof(select_date2 ) != "undefined"){
                    if( select_date1 <= date && date <= select_date2 ) {

                    }else {
                        return false;
                    }
                }else if($scope.today>date){
                    return false;
                }
                if(select_type != '3' && typeof(select_type) != "undefined"){
                    if(select_type != type){
                        return false;
                    }
                }
                if(select_bankName !='' && typeof(select_bankName) != "undefined") {
                    if (bankname.indexOf(select_bankName) < 0) {
                        return false;
                    }
                }

                $scope.yiYuanNum += (data[6] == '' ? 0 : parseInt(data[6]));
                $scope.wuJiaoNum +=  (data[7] == '' ? 0 : parseInt(data[7]));
                $scope.yiJiaoNum += (data[8] == '' ? 0 : parseInt(data[8]));
                $("#yiYuanNum").html($scope.yiYuanNum);
                $("#wuJiaoNum").html($scope.wuJiaoNum);
                $("#yiJiaoNum").html($scope.yiJiaoNum);
                return true;
            }else{
                return true;
            }
        }
    );
    $scope.check_table = function () {
        $scope.yiYuanNum = 0;$scope.wuJiaoNum = 0;$scope.yiJiaoNum = 0;
        $("#dataTables-example").DataTable().draw();
        $("#yiYuanNum").html($scope.yiYuanNum);
        $("#wuJiaoNum").html($scope.wuJiaoNum);
        $("#yiJiaoNum").html($scope.yiJiaoNum);
    }
    $scope.exportToExcel=function(tableId){ // ex: '#my-table'
        $scope.exportHref=Excel.tableToExcel(tableId,'sheet name');
        $timeout(function(){location.href=$scope.exportHref;},100); // trigger download

    }
    $scope.addBlack =function (openId) {
        dataService.get(API('huanbi/blankList/addBlankList?openId='+openId)).then(function (res) {
            if(res.successful){
                alert(res.message);
            }
        })
    }
});

app.controller('bankListCtrl',function ($rootScope, $state, $scope, $http,dataService) {
    $scope.getBankNet =function () {
        dataService.get(API('huanbi/bank/getAllBankNets')).then(function (res) {
            $scope.banks = res;
            $scope.tableList();
        })
    }
    $scope.getBankNet();
    $scope.tableList=function () {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            //下面是在table render完成后执行的js
            var table = $("#dataTables-example").dataTable({
                bJQueryUI: true,
                bSort:false,
                "sScrollX": '100%',
            });
        });
    }
    $scope.create = function (index) {
        $rootScope.bankCreateId =$scope.banks[index].banknetId;
        $state.go('home.bank_create');
    }
    $scope.del = function (index) {
        $http.get(API('huanbi/bank/deleteBankNet?id='+$scope.banks[index].banknetId)).success(function (result) {
            if(result.successful){
                alert('删除成功');
                $scope.getBankNet();
                $window.location.reload();
            }
        })
    };

    $scope.update = function (index) {
        $rootScope.bankUpdate =$scope.banks[index];
        $state.go('home.bankList_update');
    };
    $scope.add = function () {
        $state.go('home.bankList_add');
    }
});
app.controller('bankAddCtrl',function ($rootScope, $state, $scope, $http,$filter) {


    $scope.submit = function () {
        var bank = "address=" +$scope.address+
            "&phone=" +$scope.phone+
            "&contacts=" +$scope.contacts+
            "&netName="+$scope.netName+
            "&areaId="+$scope.areaId+
            "&area="+$filter('getArea')($scope.areaId);
        $http.get(API('huanbi/bank/addBankNet?'+bank)).success(function (result) {
            if(result.successful){
                alert('新增成功');
                $state.go('home.bankList');
            }else {
                alert('新增失败');
            }
        }).error(function (message) {
            alert('请求发生失败');
            console.log(message);
        });
    }
});
app.controller('bankUpdateCtrl',function ($rootScope, $state, $scope, $http,$filter) {
    $scope.bankUpdate = $rootScope.bankUpdate;
    $scope.submit = function () {
        var bankUpdate = "address=" +$scope.bankUpdate.address+
            "&phone=" +$scope.bankUpdate.phone+
            "&contacts=" +$scope.bankUpdate.contacts+
            "&netName="+$scope.bankUpdate.netName+
            "&areaId="+$scope.bankUpdate.areaId+
            "&area="+$filter('getArea')($scope.bankUpdate.areaId)+
            "&banknetId="+$scope.bankUpdate.banknetId;
        $http.get(API('huanbi/bank/modifyBankNet?'+bankUpdate)).success(function (result) {
            if(result.successful){
                alert('修改成功');
                $state.go('home.bankList');
            }else {
                alert('修改失败');
            }
        })
    }
});

app.controller('bankUserListCtrl',function ($rootScope, $state, $scope, $http) {
    $scope.getBankUser = function () {
        $http.get(API("huanbi/staff/findAllStaff")).success(function (result) {
            if (result.successful){
                $scope.bankUserList = result.data;
                $scope.tableList();
            }
        })
    }
    $scope.getBankUser();
    $scope.tableList= function() {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            //下面是在table render完成后执行的js
            var table = $("#dataTables-example").dataTable({
                bJQueryUI: true,
                bSort:false,
                "sScrollX": '100%',
            });
        });
    }
    $scope.del = function (index) {
        $http.get(API('huanbi/staff/deleteStaff?account='+$scope.bankUserList[index].account)).success(function (result) {
            if(result.successful){
                alert('删除成功');
                $scope.bankUserList.splice(index,1);
                $scope.getBankUser();
                $window.location.reload();
            }
        })
    };
    $scope.add = function () {
        $state.go('home.bankUser_add');
    }
});
app.controller('bankUserAddCtrl',function ($rootScope, $state, $scope, $http,dataService) {
    $scope.getBankNet =function () {
        dataService.get(API('huanbi/bank/getAllBankNets')).then(function (res) {
            $scope.banks = res;
        })
    }
    $scope.getBankNet();
    $scope.bankNetId = $rootScope.AdminInfo.banknetId;
    $scope.submit =function () {
        var data = "account=" + $scope.account+
            "&password=" + md5($scope.password)+
            "&staffName=" + $scope.staffName+
            "&level="+$scope.level+
            "&banknetId="+$scope.bankNetId+
            "&phone="+ $scope.phone;
        $http.get(API('huanbi/staff/addStaff?'+data)).success(function (result) {
            if(result.successful){
                alert('成功');
                $state.go('home.bankUser_list');
            }else {
                alert(res.message);
            }
        })
    };
});

app.controller('coinListCtrl',function ($rootScope, $state, $scope, $http) {
    $scope.getCoin = function(){
        $http.get(API('huanbi/coin/queryChanger')).success(function (result) {
            if(result.successful){
                $scope.coins=result.data;
                $scope.tableList();
            }
        });
    }
    $scope.getCoin();
    $scope.tableList=function () {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            //下面是在table render完成后执行的js
            var table = $("#dataTables-example").dataTable({
                bJQueryUI: true,
                bSort:false,
                "sScrollX": '100%',
            });
        });
    }
    $scope.del = function (index) {
        $http.get(API('huanbi/coin/deleteChanger?id='+$scope.coins[index].coinChangerId)).success(function (result) {
            if(result.successful){
                alert('删除成功');
                $scope.getCoin();
                $window.location.reload();
            }
        })
    };

    $scope.update = function (index) {
        $rootScope.coinUpdate =$scope.coins[index];
        $state.go('home.coin_update');
    };
    $scope.add = function () {
        $state.go('home.coin_add');
    }
});
app.controller('coinAddCtrl',function ($rootScope, $state, $scope, $http,$filter) {
    $scope.coinAdd={};
    $scope.submit = function () {
        var bank = "&area="+$scope.coinAdd.area+
            "&address=" +$scope.coinAdd.address+
            "&contactPhone=" +$scope.coinAdd.contactPhone+
            "&cunDui="+$scope.coinAdd.cunDui+
            "&netName="+$scope.coinAdd.netName;
        $http.get(API('huanbi/coin/addChanger?'+bank)).success(function (result) {
            if(result.successful){
                alert('新增成功');
                $state.go('home.coinList');
            }else {
                alert('新增失败');
            }
        }).error(function (message) {
            alert('请求发生失败');
            console.log(message);
        });
    }
});
app.controller('coinUpdateCtrl',function ($rootScope, $state, $scope, $http,$filter) {
    $scope.coinUpdate = $rootScope.coinUpdate;
    $scope.submit = function () {
        var coinUpdate = "address=" +$scope.coinUpdate.address+
            "&contactPhone=" +$scope.coinUpdate.contactPhone+
            "&cunDui=" +$scope.coinUpdate.cunDui+
            "&netName="+$scope.coinUpdate.netName+
            "&area="+$scope.coinUpdate.area+
            "&coinChangerId="+$scope.coinUpdate.coinChangerId;
        $http.get(API('huanbi/coin/modifyChanger?'+coinUpdate)).success(function (result) {
            if(result.successful){
                alert('修改成功');
                $state.go('home.coinList');
            }else {
                alert('修改失败');
            }
        })
    }
});

app.controller('suggestionCtrl',function ($rootScope, $state, $scope, $http,dataService) {
    $http.get(API('huanbi/suggestion/querySuggestion')).success(function (result) {
        if(result.successful){
            $scope.coins=result.data;
            $scope.coins.sort(function(a,b){
                return b.suggestionDate-a.suggestionDate});
        }
    });
    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        //下面是在table render完成后执行的js
        var table = $("#dataTables-example").dataTable({
            bJQueryUI: true,
            "sScrollX": '100%',
            bSort:false,
        });

    });
    $scope.addBlack =function (openId) {
        dataService.get(API('huanbi/blankList/addBlankList?openId='+openId)).then(function (res) {
            if(res.successful){
                alert(res.message);
            }
        })
    }
    $scope.setStatus =function (type) {

    }
});
app.controller('blackListCtrl',function ($scope, $http, dataService) {
    $scope.getBlack = function () {
        dataService.get(API('huanbi/blankList/getBlankList')).then(function (res) {
            $scope.blackList= res;
            $scope.tableList();
        })
    }
    $scope.getBlack();
    $scope.tableList = function () {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            //下面是在table render完成后执行的js
            var table = $("#dataTables-blackList").dataTable({
                bJQueryUI: true,
                bSort:false,
                "sScrollX": '100%',
            });
        });
    }
    $scope.del = function (index) {
        $http.get(API('huanbi/blankList/deleteBlankList?id='+$scope.blackList[index].blacklistId)).success(function (result) {
            if(result.successful){
                alert('删除成功');
                $scope.getBlack();
                $window.location.reload();
            }
        })
    };
});

app.controller('xmeListCtrl',function ($scope, $rootScope, $http,$state,dataService) {
    $scope.getXme = function () {
        dataService.get(API('huanbi/xme/queryChanger')).then(function (res) {
            if(res.successful){
                $scope.xmes=res.data;
                $scope.tableList();
            }
        })
    };
    $scope.addXme = function () {
        $state.go('home.xmeList_add');
    };
    $scope.updateXme = function (index) {
        $rootScope.xmeUpdate =$scope.xmes[index];
        $state.go('home.xmeList_update');
    };
    $scope.delXme = function (index) {
        dataService.get(API('huanbi/xme/deleteChanger?id='+$scope.xmes[index].banknetId)).then(function (res) {
            if(res.successful){
                alert(res.message);
                $scope.getXme();
                $window.location.reload();
            }
        })
    }
    $scope.getXme();
    $scope.tableList = function () {
        $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
            //下面是在table render完成后执行的js
            var table = $("#dataTables-xme").dataTable({
                bSort:false,
                bJQueryUI: true,
                "sScrollX": '100%',
            });
        });
    }
})
app.controller('xmeAddCtrl',function ($scope, $rootScope, $http,$state,$filter) {
    $scope.submit = function () {
        var bank = "address=" +$scope.address+
            "&phone=" +$scope.phone+
            "&contacts=" +$scope.contacts+
            "&netName="+$scope.netName+
            "&areaId="+$scope.areaId+
            "&area="+$filter('getArea')($scope.areaId);
        $http.get(API('huanbi/xme/addChanger?'+bank)).success(function (result) {
            if(result.successful){
                alert('新增成功');
                $state.go('home.xmeList');
            }else {
                alert('新增失败');
            }
        }).error(function (message) {
            alert('请求发生失败');
            console.log(message);
        });
    }
})
app.controller('xmeUpdateCtrl',function ($scope, $rootScope, $http,$state,$filter) {
    $scope.bankUpdate = $rootScope.xmeUpdate;
    $scope.submit = function () {
        var bankUpdate = "address=" +$scope.bankUpdate.address+
            "&phone=" +$scope.bankUpdate.phone+
            "&contacts=" +$scope.bankUpdate.contacts+
            "&netName="+$scope.bankUpdate.netName+
            "&areaId="+$scope.bankUpdate.areaId+
            "&area="+$filter('getArea')($scope.bankUpdate.areaId)+
            "&banknetId="+$scope.bankUpdate.banknetId;
        $http.get(API('huanbi/xme/modifyChanger?'+bankUpdate)).success(function (result) {
            if(result.successful){
                alert('修改成功');
                $state.go('home.xmeList');
            }else {
                alert('修改失败');
            }
        })
    }
})